const Koa = require('koa');
const router = require('koa-router')();
const render = require('koa-art-template');
const path = require('path');
const static = require('koa-static');
const admin = require('./routes/admin.js');
const api = require('./routes/api.js');
const index = require('./routes/index.js');
const session = require('koa-session');
const bodyParser = require('koa-bodyparser');

const app = new Koa();
//配置 koa-art-template 模板引擎
render(app, {
    root: path.join(__dirname, 'views'), //视图的位置
    extname: '.html', //后缀名
    debug: process.env.NODE_ENV !== 'production' //是否开启调试模式
})

//配置静态文件中间件
app.use(static(__dirname + '/public'))

//配置session中间件
app.keys = ['some secret hurr']; //cookie的签名
const CONFIG = {
    key: 'koa:sess', //cookie key (default is koa:sess)
    maxAge: 86400000, // cookie 的过期时间 maxAge in ms (default is 1 days)
    overwrite: true, //是否可以 overwrite (默认 default true)
    httpOnly: true, //cookie 是否只有服务器端可以访问 httpOnly or not (default true)
    signed: true, //签名默认 true
    rolling: true, //在每次请求时强行设置 cookie，这将重置 cookie 过期时间（默认：false）
    renew: false, //(boolean) renew session when session is nearly expired,
};
app.use(session(CONFIG, app));
// or if you prefer all default config, just use => app.use(session(app));

//配置接收post bodyParser中间件
app.use(bodyParser());

//配置路由
/* router.get('/',async (ctx)=>{
    ctx.body = 'index'
}) */

//配置子路由    层级路由
router.use(index)
router.use('/admin',admin)      //在模块里暴露并启动路由
router.use('/api',api)

//启动路由
app.use(router.routes()).use(router.allowedMethods());

app.listen(3001)